package com.sgb.kjwl.viewmodel.packers;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.FindPackersEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.fragment.packers.PackersWorkTeamFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FragPackersWorkTeamModel extends BaseViewModel {

    private PackersWorkTeamFragment fragment;
    public HomeRepertory repertory;

    private PickSelectEntity all;

    private int page = 1;

    public FragPackersWorkTeamModel(PackersWorkTeamFragment fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
        all = new PickSelectEntity();
        all.setNo("");
        all.setProjectscale("全部");
    }

    public void getNewList(String teamSize,  String firstCategoryNo, String secondCategoryNo, String thirdCategoryNo,String fourCategoryNo,String fiveCategoryNo,String sixCategoryNo, String isCompany, String cityNo, String teamType, boolean b) {
        Map<String, Object> map = new HashMap<>();
        page = b ? page + 1 : 1;
        map.put("areacode", repertory.getAreaCode());
        int limit = 10;
        map.put("limit", limit);
        map.put("page", page);
        // 工队类型 0  施工队  1  班组   2承包商
        map.put("teamType", teamType);
        //团队规模
        if (!"".equals(teamSize) && teamSize != null) {
            map.put("teamSize", teamSize);
        }
        if (!"".equals(firstCategoryNo) && firstCategoryNo != null) {
            map.put("firstCategoryNo", firstCategoryNo);
        }
        if (!"".equals(secondCategoryNo) && secondCategoryNo != null) {
            map.put("secondCategoryNo", secondCategoryNo);
        }
        if (!"".equals(thirdCategoryNo) && thirdCategoryNo != null) {
            map.put("thirdCategoryNo", thirdCategoryNo);
        }

        if (!"".equals(fourCategoryNo) && fourCategoryNo != null) {
            map.put("fourCategoryNo", fourCategoryNo);
        }

        if (!"".equals(fiveCategoryNo) && fiveCategoryNo != null) {
            map.put("fifthCategoryNo", fiveCategoryNo);
        }
        if(!"".equals(sixCategoryNo)&&sixCategoryNo!=null){
            map.put("sixthCategoryNo",sixCategoryNo);
        }

        if (!"".equals(cityNo) && cityNo != null) {
            map.put("cityNo", cityNo);
        }
        //团队类型
        if (!"".equals(isCompany) &&isCompany != null) {
            map.put("isCompany", isCompany);
        }
        DialogHelper.showProgressDialog(fragment.getContext(), null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().findTeam(map).enqueue(new Callback<BaseEntity<ListResponse<List<FindPackersEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<FindPackersEntity>>>> call, Response<BaseEntity<ListResponse<List<FindPackersEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null) {
                        fragment.showList(response.body().getData().getList(), b, response.body().getData().getPageSize() >= page);
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<FindPackersEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    public void getProTypeList() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getProTypeList(repertory.getAreaCode()).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setProData(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getSecondCategory(String cid) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getSecondCategory(cid).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity> entityList = response.body().getData();
                    entityList.add(0, all);
                    fragment.showMajorPopWindow(entityList);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getThrCategory(String cid) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getSecondCategory(cid).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getFourCategory(String cid) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getSecondCategory(cid).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void getFiveCategory(String cid) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getSecondCategory(cid).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void getSixCategory(String cid) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getSecondCategory(cid).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getGm(int type) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getTeamScale(repertory.getAreaCode(), "" + type).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity> entityList = response.body().getData();
                    fragment.showGmPopWindow(entityList);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 团队类型
     */
    public void getLx() {
        List<PickSelectEntity> list = new ArrayList<>();
        PickSelectEntity pickSelectEntity1 = new PickSelectEntity();
        PickSelectEntity pickSelectEntity2 = new PickSelectEntity();
        pickSelectEntity1.setProjectscale("个人");
        pickSelectEntity1.setNo("0");
        pickSelectEntity2.setProjectscale("企业");
        pickSelectEntity2.setNo("1");
        list.add(pickSelectEntity1);
        list.add(pickSelectEntity2);
    }

    /**
     * 团队状态
     */
    public void getZt() {
        List<PickSelectEntity> list = new ArrayList<>();
        PickSelectEntity pickSelectEntity1 = new PickSelectEntity();
        PickSelectEntity pickSelectEntity2 = new PickSelectEntity();
        pickSelectEntity1.setProjectscale("不可出工");
        pickSelectEntity1.setNo("0");
        pickSelectEntity2.setProjectscale("可出工");
        pickSelectEntity2.setNo("1");
        list.add(pickSelectEntity2);
        list.add(pickSelectEntity1);
    }

    public void getH5Url() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("constructteamdetail").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                MToast.showToast(fragment.getContext(), response.body().getMsg());

            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public String getAreaCode() {
        return repertory.getAreaCode();
    }

    // -------------------------------- 获取三级地址列表 --------------------------------

    public void getLocateCityList() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getAreaSecondAndThirdLevel(repertory.getAreaCode()).
                enqueue(new Callback<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> call, Response<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null || response.body().getState() == null) {
                            return;
                        }

                        if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            fragment.showCityPop(response.body().getData());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }
}
